package com.suixun.service.yxt.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.authority.OpServiceException;
import com.suixun.dao.TJxtSchoolMapper;
import com.suixun.dao.TJxtSiOrgMapper;
import com.suixun.dao.TblAgentSchoolMapper;
import com.suixun.dao.TblOrgnizationMapper;
import com.suixun.entity.TJxtSchool;
import com.suixun.entity.TJxtSiOrg;
import com.suixun.entity.TblAgentSchool;
import com.suixun.entity.TblOrgnization;
import com.suixun.entity.TblOrgnizationExample;
import com.suixun.service.yxt.YxtSchoolService;
import com.suixun.util.DateUtil;
import com.suixun.util.RandomUtil;
import com.suixun.util.StringUtil;
import com.suixun.util.YxtConstant;
import com.suixun.vo.YxtSchoolVO;

@Service
public class YxtSchoolServiceImpl implements YxtSchoolService {

	@Resource
	private TblOrgnizationMapper tblOrgnizationMapper;
	@Resource
	private TJxtSchoolMapper tJxtSchoolMapper;
	@Resource
	private TJxtSiOrgMapper tJxtSiOrgMapper;
	@Resource
	private TblAgentSchoolMapper tblAgentSchoolMapper;
	
	/**
	 * 新增学校
	 * <p>Title: yxtAddSchool</p>
	 * <p> 功能描述: </p>
	 * @param schoolVo
	 */
	@Override
	@Transactional
	public void yxtAddSchool(YxtSchoolVO schoolVo) {
		checkNull(schoolVo);
		TblOrgnization tblOrgnization = getTblOrgnizationByParam(schoolVo.getCity(),YxtConstant.OrgType.CITY);
		if (tblOrgnization == null) {
			throw new OpServiceException("地市不存在");
		}
		//新增的学校id为地市id+4位随机数
		String schoolId = new StringBuilder()
				.append(tblOrgnization.getUuid())
				.append(RandomUtil.generateLowerString(4)).toString();
		
		//获取地区id
		tblOrgnization = getTblOrgnizationByparentOrgUuid(tblOrgnization.getUuid(),YxtConstant.OrgType.TOWN,schoolVo.getTown());
		if (tblOrgnization == null) {
			throw new OpServiceException("地区不存在");
		}
		String regionId = tblOrgnization.getUuid();
		String district = tblOrgnization.getOrgName();
		//添加到tbl_orgnization表
		addTblOrgnizationBySchool(schoolId,schoolVo.getSchoolName(),schoolVo.getCreateUser(),regionId,schoolVo.getOrgSecondType());
		//添加到t_jxt_school表
		addTJxtSchool(schoolId,schoolVo.getSchoolName(),regionId,district,schoolVo.getOrgSecondType());
		//添加到t_jxt_si_org表
		addTJxtSiOrg(schoolId);
		//添加到tbl_agent_school表
		addTblAgentSchool(schoolId);
	}
	
	private void addTblOrgnizationBySchool(String schoolId,String schoolName,
			String createUser,String regionId,String orgSecondType) {
		TblOrgnization tblOrgnization = new TblOrgnization();
		tblOrgnization.setUuid(schoolId);
		tblOrgnization.setOrgName(schoolName);
		tblOrgnization.setOrgType(YxtConstant.OrgType.SCHOOL);
		tblOrgnization.setCreateDate(new Date());
		tblOrgnization.setCreateUser(createUser);
		tblOrgnization.setParentOrgUuid(regionId);
		tblOrgnization.setModifyLogId(schoolId);
		tblOrgnization.setOrgSort(new BigDecimal(0));
		tblOrgnization.setOrgUnitId(schoolId);
		tblOrgnization.setGroupSort(new BigDecimal(0));
		tblOrgnization.setLoginOrgId(schoolId);
		tblOrgnization.setOrgSecondType(orgSecondType);
		tblOrgnizationMapper.insertSelective(tblOrgnization);
	}
	
	private void addTJxtSchool(String schoolId,String schoolName,String regionId,String district,String schoolType) {
		TJxtSchool tJxtSchool = new TJxtSchool();
		tJxtSchool.setId(UUID.randomUUID().toString().replace("-", ""));
		tJxtSchool.setcPid(schoolId);
		tJxtSchool.setcName(schoolName);
		tJxtSchool.setcResourceid(regionId);
		tJxtSchool.setcSiid(YxtConstant.AGENT);
		tJxtSchool.setcStatus(0);
		tJxtSchool.setcDistrict(district);
		tJxtSchool.setcImporttime(DateUtil.formatDateToString(new Date(), "yyyyMMdd"));
		tJxtSchool.setcIfdelete("0");
		tJxtSchool.setcFeestatus("0");
		tJxtSchool.setcExpdate("20991231");
		tJxtSchool.setcSchstage(schoolType);
		tJxtSchoolMapper.insertSelective(tJxtSchool);
	}
	
	private void addTJxtSiOrg(String schoolId) {
		TJxtSiOrg tJxtSiOrg = new TJxtSiOrg();
		tJxtSiOrg.setId(UUID.randomUUID().toString().replace("-", ""));
		tJxtSiOrg.setcSchoolid(schoolId);
		tJxtSiOrg.setcSi(YxtConstant.AGENT);
		tJxtSiOrg.setcType(YxtConstant.BUSINESS_TYPE);
		tJxtSiOrg.setcStatus("0");
		tJxtSiOrgMapper.insertSelective(tJxtSiOrg);
	}
	
	private void addTblAgentSchool(String schoolId) {
		TblAgentSchool tblAgentSchool = new TblAgentSchool();
		tblAgentSchool.setId(RandomUtil.generateLowerString(8));
		tblAgentSchool.setAgentId(YxtConstant.AGENT_NAME);
		tblAgentSchool.setSchoolId(schoolId);
		tblAgentSchool.setInDate(new Date());
		tblAgentSchoolMapper.insertSelective(tblAgentSchool);
	}

	private TblOrgnization getTblOrgnizationByParam(String orgName,String orgType) {
		if (StringUtil.isEmpty(orgName)) {
			throw new OpServiceException("参数不能为空");
		}
		if (StringUtil.isEmpty(orgType)) {
			throw new OpServiceException("参数类型不能为空");
		}
		TblOrgnizationExample tblOrgnizationExample = new TblOrgnizationExample();
		tblOrgnizationExample.createCriteria().andOrgTypeEqualTo(orgType).andOrgNameEqualTo(orgName);
		List<TblOrgnization> tblOrgnizationList = tblOrgnizationMapper.selectByExample(tblOrgnizationExample);
		if (tblOrgnizationList.size() > 0) {
			return tblOrgnizationList.get(0);
		}
		return null;
	}
	
	private TblOrgnization getTblOrgnizationByparentOrgUuid(String parentOrgUuid,String orgType,String town) {
		if (StringUtil.isEmpty(parentOrgUuid)) {
			throw new OpServiceException("地市不能为空");
		}
		if (StringUtil.isEmpty(town)) {
			throw new OpServiceException("地区不能为空");
		}
		if (StringUtil.isEmpty(orgType)) {
			throw new OpServiceException("地区类型不能为空");
		}
		TblOrgnizationExample tblOrgnizationExample = new TblOrgnizationExample();
		tblOrgnizationExample.createCriteria().andOrgTypeEqualTo(orgType)
			.andParentOrgUuidEqualTo(parentOrgUuid).andOrgNameEqualTo(town);
		List<TblOrgnization> tblOrgnizationList = tblOrgnizationMapper.selectByExample(tblOrgnizationExample);
		if (tblOrgnizationList.size() > 0) {
			return tblOrgnizationList.get(0);
		}
		return null;
	}
	
	private void checkNull(YxtSchoolVO schoolVo) {
		if (schoolVo == null) {
			throw new OpServiceException("学校不能为空");
		}
		if (StringUtil.isEmpty(schoolVo.getSchoolName())) {
			throw new OpServiceException("学校名称不能为空");
		}
		if (StringUtil.isEmpty(schoolVo.getCity())) {
			throw new OpServiceException("地市不能为空");
		}
		if (StringUtil.isEmpty(schoolVo.getTown())) {
			throw new OpServiceException("地区不能为空");
		}
		if (StringUtil.isEmpty(schoolVo.getCreateUser())) {
			throw new OpServiceException("操作人姓名不能为空");
		}
	}

	private String checkCityIsExist(String city) {
		TblOrgnization tblOrgnization = getTblOrgnizationByParam(city,YxtConstant.OrgType.CITY);
		if (tblOrgnization == null) {
			throw new OpServiceException("地市不存在");
		}
		return tblOrgnization.getUuid();
	}
	
	private void checkTownIsExist(String town,String cityUid) {
		if (getTblOrgnizationByparentOrgUuid(cityUid,YxtConstant.OrgType.TOWN,town) == null) {
			throw new OpServiceException("地区不存在");
		}
	}
	
	private void checkSchoolNameIsExist(String schoolName) {
		TblOrgnization tblOrgnization = getTblOrgnizationByParam(schoolName,YxtConstant.OrgType.SCHOOL);
		if (tblOrgnization != null) {
			throw new OpServiceException("学校名称已存在");
		}
	}
	
	@Override
	public void yxtCheckAddSchool(YxtSchoolVO schoolVo) {
		checkNull(schoolVo);
		//判断地市是否存在
		checkTownIsExist(schoolVo.getTown(),checkCityIsExist(schoolVo.getCity()));
		//判断学校名称是否已存在
		checkSchoolNameIsExist(schoolVo.getSchoolName());
	}
	
	
}
